#定义判断输入是否为数字的函数
def intnum(a):
    while True:
        if not a.isdigit() or int(a) <= 0:
            a = input("请输入有效的值：\n")
        return 0

#定义一个购买商品的函数
def purchase(num, salary):
    question = "请问您需要买多少？(数量)\n"
    if num == 1:
        number = input(question)
        intnum(number)
        salary -= 5 * int(number)
        return salary
    elif num == 2:
        number = input(question)
        intnum(number)
        salary -= 3 * int(number)
        return salary
    elif num == 3:
        number = input(question)
        intnum(number)
        salary -= 2 * int(number)
        return salary
    elif num == 4:
        number = input(question)
        intnum(number)
        salary -= 10 * int(number)
        return salary
    elif num == 5:
        number = input(question)
        intnum(number)
        salary -= 4 * int(number)
        return salary
    elif num == 0:
        print("您已经购买了{}\n已退出系统!".format(list))
        exit(0)

#给用户列出商品列表和操作编号
shoplist = ['1.apple 5元/斤', '2.banana 3元/斤', '3.watermelon 2元/斤', '4.strawbary 10元/斤', '5.pear 4元/斤']
shop = ['apple', 'banana', 'watermelon', 'strawbary', 'pear']
salary = input("请输入你的工资：(单位：元)\n")
intnum(salary)
salary = int(salary)
print("下面是商品列表:")
print("0.exit")
for i in range(len(shoplist)):
    print(shoplist[i])


#开始接受用户输入的编号并对所购买的商品进行计算
select = input("您可以输入编号购买商品或者退出系统：\n")
#定义一个列表用于存放已购买过的商品
list = []
while True:
    if select.isdigit() and 0 <= int(select) <= 5:
        salary = purchase(int(select), salary)
        #判断余额是否不足
        if salary < 0:
            print("您的余额不足，无法购买!\n您已经购买了{}\n已退出系统!".format(list))
            exit(0)
        #将购买过的商品放入到一个列表中
        elif shop[int(select) - 1] not in list:
            list.append(shop[int(select) - 1])

        print("您还剩余", salary, "元")
        select2 = input("请问您还需要购买商品吗？\n1.退出\n2.继续购买\n")

        while True:
            intnum(select2)
            select2 = int(select2)
            if select2 != 1 and select2 != 2:
                select2 = input("请输入正确的编号：\n")
            else:
                if select2 == 1:
                    print("您已经购买了{}\n已退出系统!".format(list))
                    exit(0)
                elif select2 == 2:
                    select = input("请输入您想要购买商品的编号：\n")
                    break
    else:
        select = input("请输入有效的编号：\n")

